package com.zdtech.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间数据的处理转化
 * @author Zyl
 *
 */
@SuppressWarnings("all")
public class DateUtil {  
	/**
	 * 
	 * @param calendar	
	 * @param formater yyyy-MM-dd
	 * @return
	 */
	public static String calendarToString(Calendar calendar,String formater){
		SimpleDateFormat sdf = new SimpleDateFormat(formater);  
		String dateStr = sdf.format(calendar.getTime());  
		return dateStr;
	}
	/**
	 * 
	 * @param dateStr
	 * @param formater
	 * @return
	 * @throws ParseException
	 */
	public static Calendar stringToCalendar(String dateStr,String formater) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat(formater);  
		Date date =sdf.parse(dateStr);  
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date); 
		return calendar;
	}
	/**
	 * 
	 * @param date
	 * @param formater
	 * @return
	 */
	public static String dateToString(Date date,String formater){
		SimpleDateFormat sdf= new SimpleDateFormat(formater);  
		String dateStr=sdf.format(date);
		return dateStr;
	}
	/**
	 * 
	 * @param dateStr
	 * @param formater
	 * @return
	 * @throws ParseException
	 */
	public static Date stringToDate(String dateStr,String formater) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat(formater);  
		Date date= sdf.parse(dateStr); 
		return date;
	}
	/**
	 * 
	 * @param date
	 * @return
	 */
	public static Calendar dateToCalendar(Date date){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(date);  
		return calendar;
	}
	/**
	 * 
	 * @param calendar
	 * @return
	 */
	public static Date calendarToDate(Calendar calendar){
		Date date =calendar.getTime();
		return date;
	}
	/**
	 * 
	 * @param dateStr :yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Timestamp stringToTimestamp(String dateStr){
		Timestamp ts = Timestamp.valueOf(dateStr);
		return ts;
	}
	/**
	 * 
	 * @param date
	 * @param formater :yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static Timestamp dateToTimestamp(Date date,String formater){
		SimpleDateFormat df = new SimpleDateFormat(formater);  
		String time = df.format(date);  
		Timestamp ts = Timestamp.valueOf(time);  
		return ts;
	}
	/**
	 * 得到当前时间
	 * @param formater :yyyy-MM-dd HH:mm:ss:SSS
	 * @return
	 */
	public static String getCurrentDate(String formater){
		SimpleDateFormat sdf = new SimpleDateFormat(formater);
		return sdf.format(new Date());
	}
	/**
	 * 
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static int getWeekOfYear(String dateStr){  
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");  
		Date date;
		Calendar calendar = Calendar.getInstance(); 
		try {
			date = sdf.parse(dateStr);
			calendar.setTime(date);
			int week = calendar.get(Calendar.WEEK_OF_YEAR);
			return week;
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;  
		}
	}  
	public static int getWeekOfMonth(String dateStr){  
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");  
		Date date;
		Calendar calendar = Calendar.getInstance(); 
		try {
			date = sdf.parse(dateStr);
			calendar.setTime(date);
			int week = calendar.get(Calendar.WEEK_OF_MONTH);
			return week;
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;  
		}
	}  
	public static int getDayOfWeek(String dateStr){  
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");  
		Date date;
		Calendar calendar = Calendar.getInstance(); 
		try {
			date = sdf.parse(dateStr);
			calendar.setTime(date);
			int day = calendar.get(Calendar.DAY_OF_WEEK);  
			return day;
		} catch (ParseException e) {
			e.printStackTrace();
			return 0;  
		}
	}  
} 